package com.example.task1.ui.AllService.word;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.WordJobBean;
import com.example.task1.bean.WordMyResumeBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import retrofit2.Call;

public class WordMyResumeActivity extends AppCompatActivity {

    private EditText mostEducation;
    private EditText education;
    private EditText address;
    private EditText experience;
    private EditText individualResume;
    private EditText money;
    private TextView positionId;
    private Button btnUp;
    private Spinner spinner;
    private Map<Integer, String> collect1;
    private Map<String, Integer> collect2;
    private String job;
    private WordMyResumeBean.DataDTO rows;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_resume);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("我的简历");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        business();
    }

    private void business() {
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                job = parent.getItemAtPosition(position).toString();
                positionId.setText(job);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id",id);
                map.put("mostEducation",mostEducation.getText().toString());
                map.put("education",education.getText().toString());
                map.put("address",address.getText().toString());
                map.put("experience",experience.getText().toString());
                map.put("individualResume",individualResume.getText().toString());
                map.put("money",money.getText().toString());
                map.put("positionId",collect2.get(job));
                Call<ApiResp> jobCall = Api.api().put(MyApplication.TOKEN,"/prod-api/api/job/resume",Api.getBody(map));
                Api.send(jobCall, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show(data.getMsg());
                        finish();
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    private void initData() {
        Call<ApiResp> jobCall = Api.api().get(MyApplication.TOKEN,"/prod-api/api/job/profession/list");
        Api.send(jobCall, WordJobBean.class, new CallBack<WordJobBean>() {

            @Override
            public void success(WordJobBean data) {
                collect1 = data.getRows().stream().collect(Collectors.toMap(WordJobBean.RowsDTO::getId, WordJobBean.RowsDTO::getProfessionName));
                List<WordJobBean.RowsDTO> rows = data.getRows();
                rows.remove(rows.size()-1);
                rows.remove(rows.size()-1);
                collect2 = rows.stream().collect(Collectors.toMap(WordJobBean.RowsDTO::getProfessionName,WordJobBean.RowsDTO::getId));

                List<String> collect = data.getRows().stream().map(WordJobBean.RowsDTO::getProfessionName).collect(Collectors.toList());
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(WordMyResumeActivity.this, R.layout.support_simple_spinner_dropdown_item, collect);
                spinner.setAdapter(stringArrayAdapter);
                getResume();
            }

            @Override
            public void error(String msg) {

            }
        });

    }

    public void getResume(){
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN,"/prod-api/api/job/resume/queryResumeByUserId/" + MyApplication.user.getUserId());
        Api.send(apiRespCall, WordMyResumeBean.class, new CallBack<WordMyResumeBean>() {
            @Override
            public void success(WordMyResumeBean data) {
                rows = data.getData();
                mostEducation.setText(rows.getMostEducation());
                education.setText(rows.getEducation());
                address.setText(rows.getAddress());
                experience.setText(rows.getExperience());
                individualResume.setText(rows.getIndividualResume());
                money.setText(rows.getMoney());
                positionId.setText(collect1.get(rows.getPositionId()));
                id = rows.getId();
            }

            @Override
            public void error(String msg) {

            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        mostEducation = (EditText) findViewById(R.id.mostEducation);
        education = (EditText) findViewById(R.id.education);
        address = (EditText) findViewById(R.id.address);
        experience = (EditText) findViewById(R.id.experience);
        individualResume = (EditText) findViewById(R.id.individualResume);
        money = (EditText) findViewById(R.id.money);
        positionId =  findViewById(R.id.positionId);
        btnUp = (Button) findViewById(R.id.btn_up);
        spinner = (Spinner) findViewById(R.id.spinner);
    }
}